<?php


namespace app\common\model;

use think\Model;

class Category extends Model
{

    /**
     * 添加分类
     * @param $data
     * @return bool
     */
    public function add($data)
    {
        $result = $this->save($data);
        return $result;
    }

    /**
     * 获取分类
     * @param $data
     * @return object
     */
    public function queryPage($data)
    {
        $fields = '';
        if($data['fields']){
            $fields = $data['fields'];
        }

        $mapper = $this->field($fields);
        $mapper->where('status','<>',99);

        //父级查询
        if($data['pid']){
            $mapper->where('pid',$data['pid']);
        }

        //判断是否需要排序
        if($data['order']){
            $mapper->order($data['order']);
        }

        //判断是否需要分页
        if($data['size']){
            $result = $mapper->paginate($data['size']);
        }else{
            $result = $mapper->select();
        }

        return $result;
    }


    /**
     * 根据用户名获取用户的基本信息
     * @param $username
     * @return array|Model|null
     * @author bjy
     */
    public function getInfoByUsername($username)
    {
        $where  = [
            "username" => trim($username),
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    /**
     * 根据手机号获取用户的基本信息
     * @param string $phone 用户手机号
     * @return array|Model|null
     * @author bjy
     */
    public function getInfoByPhone($phone)
    {
        $where  = [
            "phone_number" => trim($phone),
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    /**
     * 根据id获取用户的基本信息
     * @param string $id 用户id
     * @return array|Model|null
     * @author bjy
     */
    public function getInfoById($id)
    {
        $where  = [
            "id" => intval($id),
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    /**
     * 根据id修改数据
     * @param int $id 数据id
     * @param array $data 修改的数据
     * @return bool
     * @author bjy
     */
    public function updateById($id, $data)
    {
        $where  = [
            "id" => intval($id),
        ];
        $result = $this->where($where)->save($data);
        return $result;
    }
}